package com.manjusaka.structure7.Composite;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/5.
 * 树形结构的处理
 * eg:对磁盘里的文件进行杀毒操作
 */
public class CompositePattern {
    public static void main(String[] args){
        Folder folder1=new Folder("文件夹1");
        Folder folder2=new Folder("文件夹2");
        Folder folder3=new Folder("文件夹3");
        //

        //1--》2--》3
        TextFile textFile1 = new TextFile("诛仙");
        TextFile textFile2 = new TextFile("仙逆");
        TextFile textFile3 = new TextFile("雪中悍刀行");
        TextFile textFile4 = new TextFile("将夜");

        ImageFile ImageFile1 = new ImageFile("詹姆斯");
        ImageFile ImageFile2 = new ImageFile("杜兰特");
        ImageFile ImageFile3 = new ImageFile("雷阿伦");
        ImageFile ImageFile4 = new ImageFile("卡哇伊");

        folder3.addFile(ImageFile1);
        folder3.addFile(ImageFile2);
        folder3.addFile(ImageFile3);
        folder3.addFile(ImageFile4);

        folder2.addFile(textFile1);
        folder2.addFile(textFile2);
        folder2.addFile(textFile3);
        folder2.addFile(textFile4);

        folder1.addFile(ImageFile1);
        folder1.addFile(ImageFile2);
        folder1.addFile(ImageFile3);
        folder1.addFile(ImageFile4);

        folder1.addFile(textFile1);
        folder1.addFile(textFile2);
        folder1.addFile(textFile3);
        folder1.addFile(textFile4);

        folder1.addFile(folder2);
        folder2.addFile(folder3);

        folder1.killVirus();



    }
}

abstract class AbstractFile {
    protected void addFile(AbstractFile AbstractFile) {
        System.out.println("不支持文件的添加");
    }

    protected void removeFile(AbstractFile AbstractFile) {
        System.out.println("不支持文件的删除");
    }

    protected AbstractFile getChild(int key) {
        System.out.println("不支持获取文件里面的东西");
        return null;
    }

    protected abstract void killVirus();//杀毒的操作
}

class TextFile extends AbstractFile {
    private String fileName;

    public TextFile(String fileName) {
        this.fileName = fileName;
    }

    protected void killVirus() {
        System.out.println("对文本文件"+fileName+"进行杀毒");
    }
}

class ImageFile extends AbstractFile {
    private String fileName;

    public ImageFile(String fileName) {
        this.fileName = fileName;
    }

    protected void killVirus() {
        System.out.println("对图片文件"+fileName+"进行杀毒");
    }
}

class Folder extends AbstractFile {
    private String fileName;
    private List<AbstractFile> files = new ArrayList<AbstractFile>();

    public Folder(String fileName) {
        this.fileName = fileName;
    }

    public void addFile(AbstractFile AbstractFile) {

        //System.out.println("文件的添加");
        this.files.add(AbstractFile);
    }

    public void removeFile(AbstractFile AbstractFile) {
        System.out.println("文件的删除");
        this.files.remove(AbstractFile);

    }

    public AbstractFile getChild(int key) {
        System.out.println("获取文件里面的东西");
        return files.get(key);
    }

    public void killVirus() {
        //
        System.out.println("*****************************文件夹"+fileName+"杀毒********************");
        for(AbstractFile file:files){
            file.killVirus();
        }
    }
}
